import { TAllTypes, IValue, IPosition, IValueBoolean } from '../../types';
import { AFunc } from '../types';
import { FormulaError } from '../../formula-error';
export class And extends AFunc {
  protected transformBooleanValue(val: boolean): IValueBoolean {
    return {type: 'value', dataType: 'boolean', value: val};
  }
  protected isTrue(val: IValue): IValueBoolean[] {
    if(val.dataType === 'number') {
      if(val.value === 0) {
        return [this.transformBooleanValue(false)]
      } else {
        return [this.transformBooleanValue(true)];
      }
    }
    if(val.dataType === 'string') {
      return val.value ? [this.transformBooleanValue(true)] : [this.transformBooleanValue(false)];
    }
    if(val.dataType === 'boolean') {
      if(!val.value) {
        return [this.transformBooleanValue(false)];
      } else {
        return [this.transformBooleanValue(true)];
      }
    }
    return [this.transformBooleanValue(true)];
  }
  public excute(args: TAllTypes[]): TAllTypes[] {
    if(args.length === 0) {
      throw new FormulaError('#VALUE!', {errorId: 'cd677823f89b'})
    }
    for(let item of args) {
      if(item.type === 'value') {
        if(this.isTrue(item)[0].value) {
          continue;
        } else {
          return [this.transformBooleanValue(false)]
        }
      }
      if(item.type === 'position') {
        let _vals = this.positionVal.getValue(item);
        for(let item of _vals) {
          if(!this.isTrue(item)[0].value) {
            return [this.transformBooleanValue(false)]
          }
        }
        continue;
      }
      if(item.type === 'colPosition' || item.type === 'rowPosition') {
        let _vals = this.positionVal.getValue(item);
        for(let item of _vals) {
          if(!this.isTrue(item)[0].value) {
            return [this.transformBooleanValue(false)]
          }
        }
        continue;
      }
      if(item.type === 'range') {
        let _vals = item.value.getValues();
        for(let item of _vals) {
          if(!this.isTrue(item as any)[0].value) {
            return [this.transformBooleanValue(false)]
          }
        }
        continue;
      }
      throw new FormulaError('#UNKOWN!', {errorId: '7790965cc584', value: item});
    }
    return [this.transformBooleanValue(true)];
  }
}